/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

import { ListNode } from ".";

function deleteDuplicates(head: ListNode | null): ListNode | null {
  // let pre = null;
  if (!head) return null;
  let cur = head;
  while (cur.next) {
    const next = cur.next;
    // if(!next) break;
    if (cur.val === next.val) {
      cur.next = next.next;
    } else {
      // 不一样
      cur = next;
    }
  }
  // console.log(head);
  // return cur;
  return head;
}

const list: ListNode = new ListNode(
  1,
  new ListNode(2, new ListNode(2, new ListNode(4, new ListNode(5))))
);
deleteDuplicates(list);
